package com.wswy.wzcx.ui.car.choose

import android.app.Activity
import android.arch.lifecycle.Observer
import android.content.Intent
import android.graphics.Color
import android.os.Bundle
import android.support.v4.widget.DrawerLayout
import android.support.v7.widget.LinearLayoutManager
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.timehop.stickyheadersrecyclerview.StickyRecyclerHeadersDecoration
import com.wswy.wzcx.R
import com.wswy.wzcx.model.car.CarTypeInfo
import com.wswy.wzcx.model.car.CarTypeResp
import com.wswy.wzcx.module.base.CBaseFragment
import com.wswy.wzcx.module.base.Resource
import com.wswy.wzcx.ui.Constants
import kotlinx.android.synthetic.main.fragment_choose_car_brand.*

class ChooseModelFragment : CBaseFragment() {

    companion object {
        private const val TAG = "ChooseModelFragment"
    }

    private lateinit var chooseCarViewModel:ChooseCarViewModel

    private lateinit var adapter : BrandsAdapter

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        return inflater.inflate(R.layout.fragment_choose_car_brand,container,false)
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        chooseCarViewModel = provideViewModelFromActivity(ChooseCarViewModel::class.java)

        drawerLayout.setScrimColor(Color.TRANSPARENT)
        drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED)

        indexBar.visibility = View.GONE

        adapter = BrandsAdapter(false).apply {

            setOnItemClickListener { _, _, t ->

                chooseCarViewModel.chooseCar.model = t

                t?.id?.let {
                    //openSeries(it)

                    activity?.run {

                        setResult(Activity.RESULT_OK,Intent().putExtra(Constants.EXTRA_DATA_CHOOSE_CAR,chooseCarViewModel.chooseCar))
                        finish()
                    }
                }

            }

        }
        recycler_view.layoutManager = LinearLayoutManager(context)
        recycler_view.addItemDecoration(StickyRecyclerHeadersDecoration(adapter))
        recycler_view.adapter = adapter

        chooseCarViewModel.getCurrentSeriesLiveData().observe(this, Observer {
            it?.run {
                openModel(this)
            }
        })

        chooseCarViewModel.getModelLiveData().observe(this, Observer {

            it?.run {
                if (isOk()){

                    data?.fold(arrayListOf<CarTypeInfo>()) { list, item ->
                        item.list?.let {
                            list.addAll(it)
                        }
                        list
                    }?.run {
                        adapter.setNewData(this)
                        adapter.notifyDataSetChanged()
                        if (isNotEmpty()){
                            recycler_view.scrollToPosition(0)
                        }
                    }

                }else {
                    adapter.clearData()
                    adapter.notifyDataSetChanged()
                }
            }
        })

    }

    private fun openModel(seriesId:String){

        chooseCarViewModel.getModels(seriesId)
    }
}